package com.example.demo.controller;

import com.example.demo.common.domain.Horder;
import com.example.demo.common.utils.AlipayRefund;
import com.example.demo.service.OrderMgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class OrderMgController {
    @Autowired
    private OrderMgService orderMgService;
    @Autowired
    private AlipayRefund alipayRefund;

    @RequestMapping("/toOrderManagement")
    public String ToOrderManagement(HttpSession session, Model model){

        model.addAttribute("uName",session.getAttribute("LoginUser"));
        model.addAttribute("OrderList",orderMgService.queryAllHorder());
        return "OrderManagement";
    }
    @RequestMapping("/OrdersDelete")
    @ResponseBody
    public String UsersDlete(String OrderNames){
        System.out.println("用户列表"+OrderNames);
        String[] OrderNamelist =OrderNames .split(",");
        String message= orderMgService.deleteByOrdernames(OrderNamelist);
        return message;
    }

    @RequestMapping("/OrderQuery")
    public String OrderQuery(Horder horder, Model model, HttpSession session)
    {

        System.out.println(horder.getOrderNumber()+";"+horder.getRname()+";"+horder.getHotelName());
        model.addAttribute("uName",session.getAttribute("LoginUser"));
        model.addAttribute("OrderList",orderMgService.queryOrderByTerm(horder));
        return "OrderManagement";
    }

    @RequestMapping("/updateState")
    public String UpdateRole(Horder horder){
        String message=orderMgService.updateState(horder);
        return "redirect:/toOrderManagement";
    }
    @RequestMapping("/Orderdelete")
    @ResponseBody
    public String Orderdelete(String OrderNumber){
        String message=orderMgService.deleteByOrdername(OrderNumber);
        return message;
    }
    @RequestMapping("/OrderRefund")
    @ResponseBody
    public String OrderRefund(String OrderNumber,String OrderPrice) throws Exception {
        String message="";
        System.out.println(OrderNumber+";"+OrderPrice);
        Horder horder=new Horder();
        horder.setOrderNumber(OrderNumber);
        horder.setOrderState("已退款");
        Boolean result=alipayRefund.goAlipayRefund(OrderNumber,OrderPrice);
        if (result){message="成功";
            orderMgService.updateState(horder);
        }
        else {message="失败";}
        System.out.println(message);
        return message;
    }

}
